package main

import (
	"fmt"
	"time"
	"sync"
)

var (
	m = make(map[int]uint64)
	lock sync.Mutex
)

type task struct {
	n int
}

func calc(t *task) {
	var res uint64
	res = 1
	for i := 1; i <= t.n; i++ {
		res *= uint64(i)
	}
	lock.Lock()
	m[t.n] = res  // 变量m用来存放结果，这样主线程里就能拿到m的值，操作要加锁
	lock.Unlock()
}

func main() {
	for i := 0; i < 100; i++ {
		t := &task{i}
		go calc(t)
	}
	for j := 0; j < 10; j++ {
		fmt.Printf("\r已运行%d秒", j)
		time.Sleep(time.Second)
	}
	fmt.Println("\r运行完毕，输出结果:")
	lock.Lock()
	for k, v := range m {
		if v != 0 {
			fmt.Printf("%d! = %v\n", k, v)
		}
	}
	lock.Unlock()
}